﻿using AutoMapper;
using Microsoft.AspNetCore.Identity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VM.Common.Result;
using VM.Entity.EntityMap.EnTry;
using VM.Entity.EntityMap.System.Code;
using VM.IDAL;
using VM.IDAL.System.Entry;
using VM.Common.strFormatUntil;
using VM.Common.EntityEnum;
using VM.IBLL.System.Entry;
using VM.Entity.EntityDto.EnTry;
using VM.Entity.EntityDto.System.Code;

namespace VM.BLL.System.Entry
{
    public class EntryBLL : BaseBLL, IEntryBLL
    {

        private readonly IEntryDAL _IEntryDAL;
        private readonly IMapper _IMapper;

        public EntryBLL(IEntryDAL iEntryDAL, IMapper iMapper)
        {
            _IEntryDAL = iEntryDAL;
            _IMapper = iMapper;
        }



        public string getCodeInfoByMenuId(int menuId)
        {

            throw new NotImplementedException();
        }

        /// <summary>
        /// 添加编码规则
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult insertEntry(addEntryDto obj)
        {
            sys_entry objRes = _IMapper.Map<addEntryDto, sys_entry>(obj);
            ApiResult res = new ApiResult();
            int Res = _IEntryDAL.addEntry(objRes);
            if (Res > 0)
            {
                res.code = 200;
                res.msg = $@"成功插入{Res}条数据";

            }
            else
            {
                res.code = 403;
                res.msg = $@"成功失败";

            }
            return res;
        }

        public ApiResult addUserEntryLink(int userId, string entryIds)
        {
            StrFormat sf = new StrFormat();
            int Count = sf.GetCount(",", entryIds) + 1;
            List<sys_userEntryLink> list = new List<sys_userEntryLink>();
            for (int i = 0; i < Count; i++)
            {
                addUserEntryLinkDto obj = new addUserEntryLinkDto();
                obj = perfectClass(obj, userId, ProTypeEnum.save);
                obj.entryId = Convert.ToInt32(sf.GetSplitStr(entryIds, ',', i));
                obj.userId = userId;
                list.Add(_IMapper.Map<addUserEntryLinkDto, sys_userEntryLink>(obj));
            }

            ApiResult res = new ApiResult();

            int Res = _IEntryDAL.addUserEntryLink(list);

            if (Res > 0)
            {
                res.code = 200;
                res.msg = $@"成功插入{Res}条数据";

            }
            else
            {
                res.code = 403;
                res.msg = $@"成功失败";

            }
            return res;
        }


        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="UserId"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public ApiDataResult<List<getEntryTreeListDto>> getEntryTreeList(string name, int userId)
        {
            List<getEntryTreeListDto> dataRes = _IEntryDAL.getEntryTreeListDto(name, userId);
            ApiDataResult<List<getEntryTreeListDto>> res = new ApiDataResult<List<getEntryTreeListDto>>()
            {
                code = 200,
                msg = "查询成功",
                data = dataRes
            };
            return res;

        }


        public ApiResult updateCodeRule(updateEntryDto obj)
        {
            sys_entry objSend = _IMapper.Map<updateEntryDto, sys_entry>(obj);
            ApiResult res = new ApiResult();
            int Res = _IEntryDAL.updateEntry(objSend);
            if (Res > 0)
            {
                res.code = 200;
                res.msg = $@"成功更新{Res}条数据";

            }
            else
            {
                res.code = 403;
                res.msg = $@"更新失败";

            }
            return res;
        }

        public ApiResult updateEntry(updateCodeRuleDto obj)
        {
            throw new NotImplementedException();
        }

    }
}
